
-module(integral).
-export([integrate/5,trapezoid/3,simpson/3]).

integrate(Rule, Func, A, B, Epsilon) when (B - A) =< Epsilon ->
  Rule(Func, A, B);
integrate(Rule, Func, A, B, Epsilon) when (B - A) > Epsilon ->
  M = (A + B) / 2,
  So = integrate(Rule, Func, A, M, Epsilon),
  Si = integrate(Rule, Func, M, B, Epsilon),
  So + Si.

trapezoid(Func, A, B) ->
  (B - A) * ((Func(A) + Func(B)) / 2).

simpson(Func, A, B) ->
  M = (A + B) / 2,
  ((B - A) / 6) * (Func(A) + 4 * Func(M) + Func(B)).
